<extend file='resource/view/master'/>
<block name="content">
    <ul class="nav nav-tabs" role="tablist">
        <li><a href="{{u('index')}}">商品管理</a></li>
        <li class="active"><a href="{{u('edit')}}">编辑商品</a></li>
    </ul>
    <form class="form-horizontal" onsubmit="return shopAdd" id="form" action="" method="post">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">商品列表</h3>
            </div>
            <div class="panel-body">
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">商品名称</label>
                    <div class="col-sm-9">
                        <input type="text" name="sname"  class="form-control" value="{{$oldData['sname']}}">
                    </div>
                </div>
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">所属分类</label>
                    <div class="col-sm-9">
                        <select class="js-example-basic-single form-control" name="cid" >
                            <option value="0">请选择分类</option>
                            <foreach from="$cateData" key="$k" as value="$v">
                                <option value="{{$v['cid']}}"
                                <if value="$oldData['cid']==$v['cid']">selected="selected"</if>
                                tid="{{$v['tid']}}" pid="{{$v['pid']}}">{{$v['_cname']}}</option>
                            </foreach>
                        </select>
                    </div>
                </div>
                <!--所属分类的异步-->
                <script>
                    $(function () {
                        $('select[name=cid]').change(function () {
                            var tid = $(":selected").attr('tid');
                            var pid = $(":selected").attr('pid');
                            $('input[name=tid]').val(tid);
//                            console.log($(this));return;
                            if(pid==0){
                                // alert('顶级分类不允许添加商品');
                                $(this).val(0);
                                return false;
                            }
                            $.post("{{u('ajaxGetAttr')}}",{tid:tid},function (res) {
                                var attr='';
                                var spec='';
                                attr +='<table class="table table-striped">';
                                spec +='<table class="table table-striped">';
                                $.each(res,function (k,v) {
                                    if (v.taclass==2){
                                        //当是属性时
                                        attr += '<tr><td>'+ v.taname +'</td><td>';
                                        attr += '<select name="attr['+ v.taid +']" id=""><option value="0">请选择</option>';
                                        $.each(v.tavalue,function (kk,vv) {
                                            attr +='<option value="'+vv+'">'+vv+'</option>';
                                        })
                                        attr+='</select></td></tr>';
                                    }else{
                                        //当是规格时
                                        spec +='<tr><td>'+v.taname+'</td><td>';
                                        spec +='<select name="spec['+ v.taid+'][color][]" id=""><option value="0">请选择</option>';
                                        $.each(v.tavalue,function (kk,vv) {
                                            spec += '<option value="'+vv+'">'+vv+'</option>';
                                        })
                                        spec += '</select></td><td>附加价格</td>';
                                        spec +='<td><input type="text" name="spec['+ v.taid+'][added][]"></td><td><button type="button" class="btn btn-primary btn-xs addspec">添加</button></td></tr>';
                                    }
                                })
                                attr += '</table>';
                                spec += '</table>';
                                $('#attr').html(attr);
                                $('#spec').html(spec);
                            },'json')
                        })
//                        $('select[name=cid]').change();
                    })

                </script>
                <script>
                    $(function () {
                        $("#spec").delegate('.addspec','click',function () {
                           var tr =$(this).parents('tr').clone();
                            tr.find('.addspec').remove();
                            var del ='<button type="button" class="btn btn-danger btn-xs delspec">删除</button>';
                            tr.find('td').eq(4).append(del);
                            $(this).parents('tr').after(tr);

                        })
                        $("#spec").delegate('.delspec','click',function () {
                           $(this).parents('tr').remove();
                        })
                    })
                </script>
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">所属品牌</label>
                    <div class="col-sm-9">
                        <select class="js-example-basic-single form-control" name="bid">
                            <option value="0">请选择品牌</option>
                            <foreach from="$brankData" key="$k" as value="$v">
                                <option value="{{$v['cid']}}"
                                <if value="$oldData['bid']==$v['bid']">selected="selected"</if>>{{$v['bname']}}</option>
                            </foreach>
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">商品属性</label>
                    <div class="col-sm-9" id="attr">
                        <table class="table table-striped">
                            <foreach from="$attrData" key="$k" value="$v">
                                <tr>
                                    <td>{{$v['taname']}}</td>
                                    <td>
                                        <select name="attr[{{$v['taid']}}]" id="">
                                            <option value="0">请选择</option>
                                            <foreach from="$v['tavalue']"key="$kk" value="$vv">
                                                <option <if value="in_array($vv,$shopAttrData)">selected</if> value="{{$vv}}">
                                                {{$vv}}
                                                </option>
                                            </foreach>
                                        </select>
                                    </td>
                                </tr>
                            </foreach>
                        </table>
                    </div>
                </div>
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">商品规格</label>
                    <div class="col-sm-9" id="spec">
                        <table class="table table-striped">
                            <foreach from="$specData" key="$k" value="$v">
                                <tr>
                                    <td>{{$v['taname']}}</td>
                                    <td>
                                        <select name="spec[{{$v['taid']}}][color][]" id="">
                                            <option value="0">请选择</option>
                                            <foreach from="$v['tavalue']"key="$kk" value="$vv">
                                                <option <if value="$v['stvalue']==$vv">selected</if> value="{{$vv}}">
                                                {{$vv}}
                                                </option>

                                            </foreach>
                                        </select>
                                    </td>
                                    <td>附加价格</td>
                                    <td>
                                        <input type="text" name="spec[{{$v['taid']}}][added][]" value="{{$v['added']}}">
                                    </td>
                                    <td>
                                        <button class="btn btn-success btn-xs addspec" type="button">添加</button>
                                    </td>
                                </tr>
                            </foreach>
                        </table>
                    </div>
                </div>
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">商品货号</label>
                    <div class="col-sm-9">
                        <input type="text" name="no"  class="form-control" value="{{$oldData['no']}}">
                    </div>
                </div>
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">单位</label>
                    <div class="col-sm-9">
                        <input type="text" name="sunit"  class="form-control" value="{{$oldData['sunit']}}">
                    </div>
                </div>
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">市场价</label>
                    <div class="col-sm-9">
                        <input type="text" name="marketprice"  class="form-control" value="{{$oldData['marketprice']}}">
                    </div>
                </div>
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">商品价</label>
                    <div class="col-sm-9">
                        <input type="text" name="commodityprice"  class="form-control" value="{{$oldData['commodityprice']}}">
                    </div>
                </div>
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">总库存</label>
                    <div class="col-sm-9">
                        <input type="text" name="stocks"  class="form-control" value="{{$oldData['stocks']}}">
                    </div>
                </div>
                <!-- 列表图-->
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">列表图</label>
                    <div class="col-sm-9">
                        <div class="input-group">
                            <input type="text" class="form-control" name="picture" readonly="" value="{{$oldData['picture']}}">
                            <div class="input-group-btn">
                                <button onclick="upImage(this)" class="btn btn-default" type="button" >选择图片
                                </button>
                            </div>
                        </div>
                        <div class="input-group" style="margin-top:5px;">
                            <img src="{{$oldData['picture']?:'resource/images/nopic.jpg'}}" class="img-responsive img-thumbnail liebiaotu"
                                 width="150">
                        </div>
                        <span class="help-block">建议大小(宽100高100)</span>
                    </div>
                </div>
                <!-- 商品图册-->
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">商品图册</label>
                    <div class="col-sm-9">
                        <div class="input-group">
                            <div class="input-group-btn">
                                <button onclick="manyupImage(this)" class="btn btn-default" type="button">选择图片</button>
                            </div>
                        </div>
                        <div class="input-group" id="box" style="margin-top:5px;">
                            <foreach from="$oldData['big']" key="$k" value="$v">
                                <li style="float: left;list-style: none;position:relative">
                                    <img src="{{$v}}" alt="" class="img-responsive img-thumbnail" width="150">
                                    <input type="hidden" name="logo[]" value="{{$v}}">
                                    <button type="button" class="close" onclick="removeManyImg(this)" style="position:absolute;right: 3px;top: 0px "><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                                </li>

                            </foreach>

                        </div>
                        <span class="help-block">建议大小(宽100高100)</span>
                    </div>
                </div>
                <!-- 点击次数-->
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">点击次数</label>
                    <div class="col-sm-9">
                        <input type="text" name="click"  class="form-control" value="{{$oldData['click']}}">
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label">商品详情</label>
                    <div class="col-sm-9">
                        <textarea id="intro" name="intro" style="height:300px;width:100%;">{{$oldData['intro']}}</textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">售后服务</label>
                    <div class="col-sm-9">
                        <textarea id="service" name="service" style="height:300px;width:100%;">{{$oldData['service']}}</textarea>
                    </div>
                </div>
            </div>
        </div>
        <input type="hidden" name="tid" value="oldData['tid']">
        <input type="hidden" name="sid" value="{{q('get.sid')}}">
        <button class="btn btn-primary" type="submit">确定</button>
    </form>
</block>
<script>
    //上传列表图
    function upImage(obj) {
        require(['util'], function (util) {
            util.image(function (images) {
                $("[name='picture']").val(images[0]);
                $(".liebiaotu").attr('src', images[0]);
            }, {})
        })
    }
    //百度编辑器
    require(['util'], function (util) {
        util.ueditor('container', {}, function (editor) {
            //这是回调函数 editor是百度编辑器实例
        });
    })
</script>

<script>
    //上传商品图册
    function manyupImage(obj) {
        require(['util'], function (util) {
            options = {
                //上传多图
                multiple: true,
            };
            var i=1;
            util.image(function (images) {
                i++;
                if(i==2){
                    $(images).each(function(k,v){
                        var li = '<li style="float: left;list-style: none"><img src="'+v+'" class="img-responsive img-thumbnail" width="150"><input type="hidden" name="logo[]" value="'+v+'"></li>'
                        $(li).appendTo('#box');
                    })
                }
            }, options)
        });
    }
    function removeManyImg(obj) {
        $(obj).parent().remove();
    }
</script>
<script>
    util.ueditor('service', {hash:2,data:'hd'}, function (editor) {
        //这是回调函数 editor是百度编辑器实例
    });
    util.ueditor('intro', {hash:2,data:'hd'}, function (editor) {
        //这是回调函数 editor是百度编辑器实例
    });
</script>
<script>
    function shopAdd()
    {

        var data = $("#form").serialize();
        $.post("{{u('store')}}",data,function(res){
            console.log(res);
            if(res.valid)
            {//成功
                //参数：提示消息，跳转地址，成功/失败
                util.message(res.message,"{{u('index')}}",'success');
            }else{
                util.message(res.message,"",'error');
            }
        },'json')
        return false;
    }
</script>